package cn.tedu.ymg.product.service.impl;

import cn.tedu.ymg.commons.ex.ServiceException;
import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.util.PageInfoToPageDataConverter;
import cn.tedu.ymg.commons.web.ServiceCode;
import cn.tedu.ymg.product.mapper.CaseMapper;
import cn.tedu.ymg.product.pojo.entity.Case;
import cn.tedu.ymg.product.pojo.param.CaseAddNewParam;
import cn.tedu.ymg.product.pojo.param.CaseUpdateInfoParam;
import cn.tedu.ymg.product.pojo.vo.CaseListItemVO;
import cn.tedu.ymg.product.pojo.vo.CaseStandardVO;
import cn.tedu.ymg.product.service.ICaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class CaseServiceImpl implements ICaseService {

    @Autowired
    CaseMapper CaseMapper;



    @Override
    public void addNew(CaseAddNewParam caseAddNewParam) {

        log.debug("开始处理【添加方案的业务】，参数", caseAddNewParam);

        //将方案写入到数据库中
        Case Case = new Case();
        BeanUtils.copyProperties(caseAddNewParam, Case);

        int rows = CaseMapper.insert(Case);
        if (rows !=1){
            String Case1 = "添加方案失败，服务器忙，请稍后再试！";
            log.warn(Case1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Case1);
        }
        log.debug("将新的方案数据写入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除方案】的业务，参数：{}", id);
        Case Case = CaseMapper.selectById(id);
        if (Case ==null){
            String Case1 = "删除方案失败，Id不存在";
            log.warn(Case1);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Case1);
        }

        int rows = CaseMapper.deleteById(id);
        if (rows!=1){
            String Case1 = "删除方案失败，服务器忙，请稍后再试！";
            log.warn(Case1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Case1);
        }
        log.debug("从数据库删除方案，完成！");
    }

    @Override
    public void updateInfoById(Long id, CaseUpdateInfoParam caseUpdateInfoParam) {

        log.debug("开始处理【修改方案】的业务，ID:{},新数据：{}", id, caseUpdateInfoParam);

        //检查方案是否存在，如果不存在，则抛出异常
        QueryWrapper<Case> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = CaseMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的方案数量，结果：{}", countById);
        if (countById == 0) {
            String Case = "修改方案失败，方案数据不存在！";
            log.warn(Case);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Case);
        }

        //执行修改
        Case Case = new Case();
        BeanUtils.copyProperties(caseUpdateInfoParam, Case);
        Case.setId(id);
        int rows = CaseMapper.updateById(Case);
        if (rows!=1){
            String Case1 = "修改方案失败，服务器忙，请稍后再试！";
            log.warn(Case1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Case1);
        }
        log.debug("从数据库修改方案，完成！");
    }

    @Override
    public CaseStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询方案】的业务，参数：{}", id);
        CaseStandardVO queryResult = CaseMapper.getStandardById(id);
        if (queryResult==null){
            String Case = "查询方案失败，方案数据不存在！";
            log.warn(Case);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,Case);
        }
        return queryResult;
    }

    @Override
    public PageData<CaseListItemVO> list(Integer pageNum) {
        Integer pageSize = 6;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<CaseListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询方案列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<CaseListItemVO> list =  CaseMapper.list();
        PageInfo<CaseListItemVO> pageInfo = new PageInfo<>(list);
        PageData<CaseListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<CaseListItemVO> getUrlById(Long id) {
        log.debug("开始处理【根据ID查询方案类型图片】的业务，参数：{}", id);
        QueryWrapper<Case> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = CaseMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的方案数量，结果：{}", countById);
        if (countById == 0) {
            String Case = "查询方案类型图片失败，方案id不存在！";
            log.warn(Case);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Case);
        }

        List<CaseListItemVO> list = CaseMapper.getUrlById(id);
        PageInfo<CaseListItemVO> pageInfo = new PageInfo<>(list);
        PageData<CaseListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
